<template>
    <div class="body">
        <page-aside icon="icon-yisheng" title="智能健康助理" desc="随时随地为您提供健康咨询，解答疑问，关注健康。">
            <template #interact>
            </template>
        </page-aside>
        <main class="page__main">
            <!-- 会话记录 -->
            <section class="main__chat">
                <div class="chat__record">
                    <div 
                        class="record__item"
                        v-for="(item, index) in messageList.filter(item => item.role != 'system')"
                        :key="index" 
                        :class="{
                            'record__item--user': item.role == 'user',
                            'record__item--assistant': item.role == 'assistant'
                        }"
                    >
                        <!-- 内容 -->
                        <div
                            :class="{
                                'record__content--user': item.role == 'user',
                                'record__content--assistant': item.role == 'assistant'
                            }" 
                            class="record__content"
                            v-loading="item.loading"
                        >
                            <div class="markdown-here-wrapper" v-html="marked(item.content)"></div>
                        </div>
                    </div>
                </div>
            </section>
            <footer class="main__ft">
                <div class="sticky-bar">
                    <div class="form-group input-field">
                        <el-input 
                            v-model="ask"
                            type="textarea"
                            resize="none"
                            :autosize="{ minRows: 1, maxRows: 4 }"
                            placeholder="请输入您的问题"
                            class="input-textarea"
                        />
                        <el-button circle :icon="Microphone" type="primary">
                        </el-button>
                    </div>
                    <el-button 
                            @click="askAI()"
                            type="primary"
                            :disabled="!ask.trim()"
                            :icon="Promotion"
                            circle
                            size="large"
                        >
                        </el-button>
                </div>
                <div class="tips">
                        服务生成的所有内容均由人工智能模型生成，其生成内容的准确性和完整性无法保证，不代表我们的态度或观点
                </div>
            </footer>
        </main>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { Microphone, Promotion } from '@element-plus/icons-vue'
import { marked } from 'marked'
import request from '../utils/request'
import PageAside from '../components/PageAside.vue'
import '../assets/styles/markdown.scss';

const ask = ref('');

const messageList = reactive([
    {
        role: 'system',
        content: '你是一个智能健康助理，可以为用户提供健康咨询服务。',
        loading: false
    },
    {
        role: 'assistant',
        content: `### 你好，我是您的智能健康助理，很高兴为您服务！\n请问有什么可以帮助您的？我可以：\n1. 回答您的健康咨询问题；\n2. 为您提供健康建议；\n3. 为您推荐医疗资源。`,
        loading: false
    }
]); // 消息列表

//询问AI的函数
function askAI() {
    try {
        // 插入用户消息
        const askContent = ask.value;
        messageList.push({ role: 'user', content: askContent });
        ask.value = '';
        // AI消息占位，显示loading，发送请求
        messageList.push({ role: 'assistant', content: 'AI 思考中，请稍后', loading: true });
        request.post("ai/askAssistant", { message: askContent }).then((res) => {
            if (res.code === 200) { // 请求成功
                console.log(res.data);
                messageList[messageList.length - 1].content = res.data.message;
                messageList[messageList.length - 1].loading = false;
            } else {
                messageList[messageList.length - 1].content = 'AI助理出错，请稍后再试';
                messageList[messageList.length - 1].loading = false;
            }
        });
    } catch (error) {
        // 捕获同步代码块中的错误
        console.error("同步代码块出错：", error);
    }
}
</script>

<style lang="scss" scoped>
$page-bg-color: #f5f5f5;
$chat-bg-color: #f5f5f5;
$user-bg-color: #0084ff;
$assistant-bg-color: #f1f0f0;
$user-text-color: #f5f5f5;
$assistant-text-color: #333333;
$border-color: #cccccc;
$focus-border-color: $user-bg-color;
$disabled-bg-color: $border-color;

.body {
    display: flex;
    height: 95vh;
}

.page__main {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 70vw;
    background-color: $page-bg-color;
}

.main__chat {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 8rem 0;
}

.chat__record {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.record__item {
    display: flex;
    max-width: 70%;

    &--user {
        justify-content: flex-end;
        align-self: flex-end;
    }

    &--assistant {
        justify-content: flex-start;
        align-self: flex-start;
    }
}

.record__content {
    padding: 10px 15px;
    border-radius: 12px;
    background-color: $chat-bg-color;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    font-size: 14px;
    line-height: 1.5;

    &--user {
        background-color: $user-bg-color;
        color: $user-text-color;
    }

    &--assistant {
        background-color: $assistant-bg-color;
        color: $assistant-text-color;
    }
}

.main__ft {
    min-height: 10vh;
    box-sizing: border-box;
    padding: 1rem 4rem 0;
    background-color: $chat-bg-color;
}

.sticky-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.input-field {
    display: flex;
    align-items: end;
    flex: 1;
    padding: 10px 15px;
    border: 2px solid $border-color;
    border-radius: 20px;
    font-size: 18px;
    margin-right: 15px;

    &--focus {
        border-color: $focus-border-color;
    }
}

.input-textarea {
    margin-right: 10px;
}

.tips {
    font-size: 12px;
    color: #999;
    margin: 10px;
    text-align: center;
}
</style>

<style>
.input-textarea .el-textarea__inner, 
.input-textarea .el-textarea__inner:focus,
.input-textarea .el-textarea__inner:hover {
    border: none;
    box-shadow: none;
    background-color: transparent;
    color: #333;
    font-size: 14px;
    line-height: 1.5;
    resize: none;
}
</style>